import { defineStore } from 'pinia'
import { StorageCon } from '~/constant'
import { getCommits } from '~/services'
type List = {
    stus: Student[]
}

const mergeList = (list1: Object, list2: Object) => {
  const keys = Object.keys(list2)
  // eslint-disable-next-line no-debugger
  // debugger
  keys.forEach(key => {
    const originArr = list1[key] // 现有的
    const arr = list2[key] // 最新的
    if (originArr) {
      arr.forEach(it => {
        const t = originArr.find(i => i?.sha === it.sha)
        if (!t) {
          originArr.push(it)
        }
      })
    } else {
      list1[key] = list2[key]
    }
  })
  return list1
}
export default defineStore('stu', {
  // arrow function recommended for full type inference
  state: (): List => ({
    stus: JSON.parse(localStorage.getItem(StorageCon.stu)) || []
  }),
  actions: {
    // 整体更新
    save (stus) {
      this.stus = stus
      localStorage.setItem(StorageCon.stu, JSON.stringify(this.stus))
    },
    // 单个添加
    add (stu: Student) {
      this.stus.push(stu)
      localStorage.setItem(StorageCon.stu, JSON.stringify(this.stus))
    },
    del (stuName: string) {
      const idx = this.stus.findIndex(it => it.name === stuName)
      if (idx >= 0) {
        this.stus.splice(idx, 1)
      }
      localStorage.setItem(StorageCon.stu, JSON.stringify(this.stus))
    },
    // 设置加载状态
    setLoad (isLoading:boolean = true, stuName:string = '') {
      const stus = this.stus
      if (stuName === '') {
        stus.forEach(it => { it.isLoading = isLoading })
      } else {
        const t = stus.find(it => it.stuName === stuName)
        if (t) {
          t.isLoading = isLoading
        }
      }
    },
    // 更新某个同学的提交记录
    update (stu: Student, list: Object) {
      const cur = this.stus.find(i => i.name === stu.name)
      if (cur) {
        if (cur.list) {
          // 合并 list
          cur.list = mergeList(cur.list, list)
        } else {
          cur.list = list
        }
      }
      // 持久化
      localStorage.setItem(StorageCon.stu, JSON.stringify(this.stus))
    },
    async refresh (stuName: string) {
      const idx = this.stus.findIndex(i => i.name === stuName)
      this.setLoad(true, stuName)
      this.stus[idx].isLoading = true
      try {
        const { data } = await getCommits(this.stus[idx].giteeName, this.stus[idx].projectGiteeName)
        mergeList(this.stus[idx].list, data)
        this.stus[idx].latestVisitTimeStamp = Date.now()
      } finally {
        this.stus[idx].isLoading = false
        // 持久化
        localStorage.setItem(StorageCon.stu, JSON.stringify(this.stus))
      }
    }
    // 整体更新
    // update (commitList) {
    //   this.latestStamp = Date.now()

    //   commitList.forEach(it => {
    //     const cur = this.stus.find(i => i.name === it.name)
    //     if (!cur) {
    //       // 合并 list
    //       cur.list = mergeList(cur.list, it.list)
    //     }
    //   })
    //   this.commitList = commitList
    //   localStorage.setItem(StorageCon.com, JSON.stringify(this.commitList))
    // }
  }
})
